package com.example.stock.ui.order;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;

import com.example.stock.R;
import com.example.stock.base.BaseActivity;
import com.example.stock.base.CommonCallback;
import com.example.stock.base.Result;
import com.example.stock.config.Config;
import com.example.stock.databinding.ActivityGoodsBinding;
import com.example.stock.model.GoodsOrderBean;
import com.example.stock.ui.main.MainActivity;
import com.example.stock.ui.query.QueryActivity;
import com.example.stock.ui.scan.AutoQueryActivity;
import com.example.stock.util.DBUtil;
import com.example.stock.util.DialogUtils;
import com.example.stock.util.Utils;
import com.google.android.material.snackbar.Snackbar;

import java.util.List;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import per.goweii.actionbarex.common.OnActionBarChildClickListener;

public class GoodsActivity extends BaseActivity<ActivityGoodsBinding, GoodsViewModel> {
    private static final String EXTRA_TYPE = "type";
    private int mType;
    private GoodItemAdapter mItemAdapter;

    public static void start(Context context, int type) {
        Intent intent = new Intent(context, GoodsActivity.class);
        intent.putExtra(EXTRA_TYPE, type);
        context.startActivity(intent);
    }

    @Override
    protected Class<GoodsViewModel> initViewModel() {
        return GoodsViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_goods;
    }

    @Override
    protected void initView() {
        mType = getIntent().getIntExtra(EXTRA_TYPE, Config.GOOD_TYPE_QUERY);
        mItemAdapter = new GoodItemAdapter(mType == Config.GOOD_TYPE_UPLOAD, mType);
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mBinding.recyclerView.setAdapter(mItemAdapter);
        switch (mType) {
            case Config.GOOD_TYPE_QUERY:
                mBinding.abc.getTitleTextView().setText("选择需要查询的货单");
                break;
            case Config.GOOD_TYPE_UPLOAD:
                mBinding.abc.getTitleTextView().setText("选择需要上传的货单");
                break;
            case Config.GOOD_TYPE_SCAN:
                mBinding.abc.getTitleTextView().setText("选择需要扫码的货单");
                break;
        }
        mBinding.abc.getRightTextView().setVisibility(View.GONE);
        mBinding.abc.getRightTextView().setTextColor(Color.WHITE);
        mBinding.abc.getRightTextView().setText("确认");
        mItemAdapter.setListener(new CommonCallback<Object>() {
            @Override
            public void onResult(Object... data) {
                GoodsOrderBean orderBean = (GoodsOrderBean) data[0];
                if (orderBean == null) {
                    Snackbar.make(mBinding.recyclerView, "请选择货单！", Snackbar.LENGTH_LONG).setDuration(1000).show();
                    return;
                }
                switch (mType) {
                    case Config.GOOD_TYPE_QUERY:
                        QueryActivity.start(GoodsActivity.this, orderBean);
                        break;
                    case Config.GOOD_TYPE_UPLOAD:
                        if (orderBean.getIsUpload() > 0) {
                            Utils.getInstance().showToast("货单已上传");
                            return;
                        }
                        DialogUtils.showInfoDialog(GoodsActivity.this, "确定更新货单" + orderBean.getDjh() + "到服务器吗？", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                DialogUtils.closeInfoDialog();
                                mViewModel.upload(orderBean);
                            }
                        }, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                DialogUtils.closeInfoDialog();
                            }
                        });
                        break;
                    case Config.GOOD_TYPE_SCAN:
                        if (orderBean.getIsUpload() > 0) {
                            Utils.getInstance().showToast("货单已上传");
                            return;
                        }
                        AutoQueryActivity.start(GoodsActivity.this, orderBean);
                        break;
                }
            }
        });

        mViewModel.getGoodOrders();
    }

    @Override
    protected void registerObserve() {
        super.registerObserve();
        mViewModel.goods.observe(this, new Observer<Result<List<GoodsOrderBean>>>() {
            @Override
            public void onChanged(Result<List<GoodsOrderBean>> listResult) {
                if (listResult.isSuccess()) {
                    mItemAdapter.setData(listResult.getData());
                } else {
                    Utils.getInstance().showToast(listResult.getMsg());
                    finish();
                }
            }
        });
        mViewModel.uploadResult.observe(this, new Observer<Result<GoodsOrderBean>>() {
            @Override
            public void onChanged(Result<GoodsOrderBean> result) {
                Utils.getInstance().showToast(result.getMsg());
                if (result.isSuccess()) {
                    GoodsOrderBean data = result.getData();
                    data.setIsUpload(1);
                    DBUtil.getInstance().update(data);
                    finish();
                }
            }
        });
    }
}
